package config

import (
	"os"

	"github.com/pelletier/go-toml"
)

type OSS struct {
	AccessID     string `toml:"access_id"`
	Endpoint     string `toml:"endpoint"`
	AccessSecret string `toml:"access_secret"`
	Bucket       string `toml:"bucket"`
}

type Config struct {
	Node        int64  `toml:"node"`
	Address     string `toml:"address"`
	OSS         *OSS   `toml:"oss"`
	AccessPoint string `toml:"access_point"`
	KeepLocal   bool   `toml:"keep_local"`
	FileRoot    string `toml:"file_root"`
}

func FromFile(path string) (*Config, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var c Config
	if err := toml.NewDecoder(file).Decode(&c); err != nil {
		return nil, err
	}

	return &c, nil
}
